package com.sinodata.ns.backup;

import com.sinodata.ns.cell.CellManager;

import jt.classic.system.ISystem;
import jt.classic.system.SimpleAction;
import jt.classic.system.database.SystemDataSource;

import net.sf.json.JSONArray;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.limp.basework.impl.CommonBean4HSQ;

import org.limp.mine.Label;
import org.limp.mine.QueryJack;
import org.limp.mine.QueryJackFactory;
import org.limp.mine.StringTool;

import java.sql.Connection;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 数据查询action
 *
 * @author mido
 *
 */
public class DataQueyAction extends SimpleAction {
    /**
        * 历史轨迹数据查询
        * @param mapping
        * @param form
        * @param request
        * @param response
        * @return ActionForward
        * @throws Exception
        */
    public ActionForward querytrack(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        CommonBean4HSQ req = new CommonBean4HSQ(request);

        Map param = req.getResource();

        String qtable = StringTool.checkString(param.get("qtable"));

        if (qtable.equals("")) {
            String jsonString = "{\"pageNO\":\"1\",\"totalPage\":\"0\",\"totalCount\":\"0\",\"queryList\":[]}";
            request.setAttribute("result", jsonString);
        } else {
            String pageNO = StringTool.checkString(param.get("pageNO"));

            String start = StringTool.checkString(param.get("start"), "0");

            String size = StringTool.checkString(param.get("limit"), "20");

            pageNO = String.valueOf((Integer.parseInt(start) / Integer.parseInt(
                        size)) + 1);

            StringBuffer where = new StringBuffer(" WHERE 1 = 1 ");

            String quserid = StringTool.checkString(param.get("quserid"));
            String qusername = StringTool.checkString(param.get("qusername"));
            String qdate = StringTool.checkString(param.get("qdate"));

            if (!quserid.equals("")) {
                where.append(" AND USERID = '" + quserid + "' ");
            }

            if (!qdate.equals("")) {
                where.append(" AND CTIME LIKE '%" + qdate + "%' ");
            }

            String sql1 = "SELECT COUNT(*) FROM " + qtable + where.toString();

            String sql2 = "SELECT * FROM " + qtable + where.toString() +
                " ORDER BY CTIME DESC";

            String url = jt.classic.system.ISystem.getContextPath() +
                "/app/dataquery.do?method=querytrack";

            try {
                SystemDataSource ds = SystemDataSource.getInstance();
                conn = ds.getConnection();

                QueryJack plus = QueryJackFactory.getInstance().createQueryJack(false);

                plus.setParameter(QueryJackFactory.PAGINATION_SQL1, sql1);
                plus.setParameter(QueryJackFactory.PAGINATION_SQL2, sql2);
                plus.setParameter(QueryJackFactory.DATABASE_CONNECTION, conn);
                plus.setParameter(QueryJackFactory.PAGINATION_LINKSTRING, url);
                plus.setParameter(QueryJackFactory.PAGINATION_COUNT, size);
                plus.setParameter(QueryJackFactory.PAGINATION_PAGENO, pageNO);

                // plus.setParameter(QueryJackFactory.PAGINATION_UPDATEPAGENO, "");
                List resultList = plus.work();

                Label label = plus.getLabel();

                JSONArray jsonArray = JSONArray.fromObject(resultList);

                String jsonString = "{\"pageNO\":\"" + label.current() +
                    "\",\"totalPage\":\"" + label.totalfan() +
                    "\",\"totalCount\":\"" + label.totaldata() +
                    "\",\"queryList\":";
                jsonString += jsonArray.toString();
                jsonString += "}";

                request.setAttribute("result", jsonString);
            } catch (Exception ex) {
                request.setAttribute("result", "{success: false}");
                ISystem.catchException(request, this, ex);
            } finally {
                SystemDataSource.closeConnection(conn);
            }
        }

        return mapping.findForward("jsonresult");
    }
}
